package com.ideaaedi.zoo.example.springboot.dynamic.ds.shardingsphere.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ideaaedi.zoo.example.springboot.dynamic.ds.shardingsphere.mapper.DemoMapper;
import com.ideaaedi.zoo.example.springboot.dynamic.ds.shardingsphere.mapper.PayRecordMapper;
import com.ideaaedi.zoo.example.springboot.dynamic.ds.shardingsphere.mapper.TransRecordMapper;
import com.ideaaedi.zoo.example.springboot.dynamic.ds.shardingsphere.mapper.UserInfoMapper;
import com.ideaaedi.zoo.example.springboot.dynamic.ds.shardingsphere.po.sharding.DemoShardingDTO;
import com.ideaaedi.zoo.example.springboot.dynamic.ds.shardingsphere.po.sharding.PayRecordPO;
import com.ideaaedi.zoo.example.springboot.dynamic.ds.shardingsphere.po.sharding.TransRecordPO;
import com.ideaaedi.zoo.example.springboot.dynamic.ds.shardingsphere.po.sharding.UserInfoPO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class BizShardingService {
    
    @Resource
    TransRecordMapper transRecordMapper;
    
    @Resource
    PayRecordMapper payRecordMapper;
    
    @Resource
    UserInfoMapper userInfoMapper;
    
    @Resource
    DemoMapper demoMapper;
    
    @DS("sharding")
    public void insertSomeShardingData() {
        prepareData();
    }
    
    /**
     * trans_record JOIN pay_record JOIN user_info
     */
    @DS("sharding")
    public List<DemoShardingDTO> listAll() {
        return demoMapper.listAll();
    }
    
    @DS("sharding")
    public void clear() {
        // 清空数据
        transRecordMapper.delete(null);
        payRecordMapper.delete(null);
        userInfoMapper.delete(null);
    }
    
    void prepareData() {
        long userId = IdWorker.getId();
        long transId = IdWorker.getId();
        
        // 插入数据
        transRecordMapper.insert(TransRecordPO.builder()
                .id(IdWorker.getId())
                .transDate("2024-03-06")
                .remark("备注X")
                .userId(100L)
                .build());
        transRecordMapper.insert(TransRecordPO.builder()
                .id(transId)
                .transDate("2024-05-06")
                .remark("备注A")
                .userId(userId)
                .build());
        payRecordMapper.insert(PayRecordPO.builder()
                .id(IdWorker.getId())
                .payDate("2024-05-06")
                .amount(new BigDecimal("2.5"))
                .transId(transId)
                .build());
        userInfoMapper.insert(UserInfoPO.builder()
                .id(userId)
                .name("张三")
                .build());
    
        transId = IdWorker.getId();
        userId = IdWorker.getId();
        transRecordMapper.insert(TransRecordPO.builder()
                .id(transId)
                .transDate("2024-06-06")
                .remark("备注B")
                .userId(userId)
                .build());
        payRecordMapper.insert(PayRecordPO.builder()
                .id(IdWorker.getId())
                .payDate("2024-06-06")
                .amount(new BigDecimal("8.3"))
                .transId(transId)
                .build());
        payRecordMapper.insert(PayRecordPO.builder()
                .id(IdWorker.getId())
                .payDate("2024-07-06")
                .amount(new BigDecimal("8.4"))
                .transId(transId)
                .build());
        payRecordMapper.insert(PayRecordPO.builder()
                .id(IdWorker.getId())
                .payDate("2024-10-06")
                .amount(new BigDecimal("8.5"))
                .transId(transId)
                .build());
        userInfoMapper.insert(UserInfoPO.builder()
                .id(userId)
                .name("李四")
                .build());
    }
}
